package fr.aracore.domotique.zapi.jmx;

import java.lang.management.ManagementFactory;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;

public class JMXManager {
	private static JMXManager _jmxManager;
	public static JMXManager defaultManager(){
		if (_jmxManager==null) 
			_jmxManager=new JMXManager();
		return _jmxManager;
	}
	
	public JMXManager() {
		
	}
	
	public void start(){
		try {
			ObjectName name = new ObjectName("fr.aracore.domotique.zapi.jmx:type=JMXDeviceManagerMBean");
			registerMBean(new JMXDeviceManager(), name);
			while (true){
					Thread.sleep(10000);
			}
				
		} catch (MalformedObjectNameException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			e.printStackTrace();
		} catch (InstanceAlreadyExistsException e) {
			e.printStackTrace();
		} catch (MBeanRegistrationException e) {
			e.printStackTrace();
		} catch (NotCompliantMBeanException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void registerMBean(Object o,ObjectName name) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException{
		MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
		mbs.registerMBean(o, name);
	}
	
}
